home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -in_the_mag- / emulation / consoles / amigavgb / gblist.c < prev    next >
C/C++ Source or Header  |  1997-12-12  |  5KB  |  161 lines

  1. /** GameBoy Cartridge Lister/Tester **************************/   
  2. /**                                                         **/
  3. /**                         gblist.c                        **/
  4. /**                                                         **/
  5. /** This program will list and [optionally] test a group of **/
  6. /** cartridges finding those which fail CRC or complement   **/
  7. /** check, or have invalid size.                            **/
  8. /**                                                         **/
  9. /** Copyright (C) Marat Fayzullin 1996                      **/
  10. /**     You are not allowed to distribute this software     **/
  11. /**     commercially. Please, notify me, if you make any    **/
  12. /**     changes to this file.                               **/
  13. /*************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. typedef unsigned char byte;
  19. typedef unsigned short word;
  20.  
  21. int CheckCRC=0,UseANSI=0,FixCRC=0;
  22.  
  23. void Error(char *File,char *Text)
  24. {
  25.   printf
  26.   (
  27.     "|%-20s|%s %-48s %s|\n",
  28.     File,UseANSI? "\033[31m":"",Text,UseANSI? "\033[0m":""
  29.   );
  30. }
  31.  
  32. int main(int argc,char *argv[])
  33. {
  34.   static char *Types[] =
  35.   { "R...","R..1","RW.1","RWB1","????","R..2","R.B2" };
  36.   static char *RAMSizes[] =
  37.   { " 0kB"," 2kB"," 8kB","32kB","    " };
  38.  
  39.   static byte Buf[0x4000];
  40.   char S[15];
  41.   FILE *F;
  42.   int J,I,K;
  43.  
  44.   char Name[50],*Type,Comment[1024];
  45.   word Checksum,Producer,RealCRC;
  46.   byte Version,Complement,RealComp;
  47.   int RAMSize,ROMBanks;
  48.  
  49.   if(argc<2)
  50.   {
  51.     fprintf(stderr,"GBLIST .GB File Processor by Marat Fayzullin\n");
  52.     fprintf(stderr,"Usage: %s [-caf] files...\n", argv[0]);
  53.     fprintf(stderr,"    -a - Use ANSI escape sequences for colors\n");
  54.     fprintf(stderr,"    -c - Check CRC\n");
  55.     fprintf(stderr,"    -f - Fix CRC\n");
  56.     return(1);
  57.   }
  58.  
  59.   CheckCRC=UseANSI=FixCRC=0;
  60.  
  61.   for(I=1;I<argc;I++)
  62.     if(*argv[I]=='-')
  63.       for(J=1;argv[I][J];J++)
  64.         switch(argv[I][J])
  65.         {
  66.           case 'c': CheckCRC=1;break;
  67.           case 'a': UseANSI=1;break;
  68.           case 'f': FixCRC=1;break;
  69.           default:
  70.             fprintf(stderr,"%s: Unknown option -%c\n",argv[0],argv[I][J]);
  71.         }
  72.  
  73.   puts("+--------------------+------------------+----+-----+----+----+--+-------+");
  74.   puts("| File               | Name             |Type| ROM |RAM |Make|VE|CRC-Cmp|");
  75.   puts("+--------------------+------------------+----+-----+----+----+--+-------+");
  76.  
  77.   for(J=(*argv[1]=='-')? 2:1;J<argc;J++)
  78.     if(*argv[J]!='-')
  79.       if(!(F=fopen(argv[J],"rb"))) Error(argv[J],"Couldn't open file");
  80.       else
  81.       {
  82.         if(fread(Buf,1,0x4000,F)!=0x4000)
  83.           Error(argv[J],"Couldn't read header");
  84.         else
  85.         {
  86.           Comment[0]='\0';
  87.  
  88.           for(I=0x134;(I<0x144)&&Buf[I];I++)
  89.             Name[I-0x134]=(Buf[I]>' ')? Buf[I]:' ';
  90.           Name[I-0x134]='\0';
  91.  
  92.           if((Buf[0x0147]<=7)&&(Buf[0x0147]!=4))
  93.           {
  94.             Type=Types[Buf[0x0147]];
  95.             ROMBanks=2<<Buf[0x148];
  96.             RAMSize=((Buf[0x0147]!=2)&&(Buf[0x0147]!=3))? 4:Buf[0x149]&0x03;
  97.           }
  98.           else
  99.           {
  100.             Type="????";ROMBanks=1;RAMSize=4;
  101.             strcat(Comment,"[Invalid Type]");
  102.           }
  103.           strcpy(S,RAMSizes[RAMSize]);
  104.           Checksum=((word)Buf[0x14E]<<8)+Buf[0x14F];
  105.           Complement=Buf[0x14D];
  106.           Producer=((word)Buf[0x14B]<<8)+Buf[0x14A];
  107.           Version=Buf[0x14C];
  108.  
  109.           /*** Checking the cartridge complement, CRC, size: ***/
  110.           if(CheckCRC)
  111.           {
  112.             for(I=0x134,RealComp=25;I<0x14D;I++) RealComp+=Buf[I];
  113.             RealComp=0x100-RealComp;
  114.             if(RealComp!=Complement)
  115.               sprintf
  116.               (
  117.                 Comment+strlen(Comment),
  118.                 "Wrong Complement: %02X!=%02X.",
  119.                 RealComp,Complement
  120.               );
  121.  
  122.             RealCRC=-Buf[0x14E]-Buf[0x14F];
  123.             for(K=0;K<0x4000;K++) RealCRC+=Buf[K];
  124.             for(I=ROMBanks-1;I>0;I--)
  125.               if(fread(Buf,1,0x4000,F)!=0x4000) break;
  126.               else for(K=0;K<0x4000;K++) RealCRC+=Buf[K];
  127.  
  128.             if(I) strcat(Comment,"[File Too Short]");
  129.             else
  130.               if(fgetc(F)!=EOF) strcat(Comment,"File is too long. ");
  131.               else
  132.                 if(Checksum!=RealCRC)
  133.                   sprintf
  134.                   (
  135.                     Comment+strlen(Comment),
  136.                     "Wrong CRC: %04X!=%04X.",
  137.                     RealCRC,Checksum
  138.                   );
  139.           }
  140.  
  141.           /*** Printing out the information line ***/
  142.           if(*Comment)
  143.             printf
  144.             (
  145.               "|%-20s|%s %-48s %s|\n",
  146.               argv[J],UseANSI? "\033[35m":"",Comment,UseANSI? "\033[0m":""
  147.             );
  148.           else       
  149.             printf
  150.             (
  151.               "|%-20s| %-16s |%s|%3dkB|%4s|%04X|%2d|%04X-%02X|\n",
  152.               argv[J],Name,Type,ROMBanks*16,S,Producer,Version,
  153.               Checksum,Complement
  154.             );
  155.         }
  156.         fclose(F);
  157.       }
  158.  
  159.   puts("+--------------------+------------------+----+-----+----+----+--+-------+");
  160. }
  161.